package com.ruoyi.project.system.async;

import com.ruoyi.project.system.role.domain.Role;
import com.ruoyi.project.system.role.service.IRoleService;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * @program: test
 * @description: 测试异步调用
 * @author: yanghua
 * @create: 2021-09-14 15:29
 **/
@EnableAsync
@RestController
@RequestMapping("/testasyc")
public class TestAsyncController {

    @Autowired
    private IUserService userService;

    @Autowired
    private IRoleService roleService;



    /**
     * A方法
     * 代码的业务逻辑应该写在service,这里就偷个懒了
     * @param name
     * @param email
     * @return
     */
    @GetMapping(value = "/saveone") //相当于@RequestMapping(value = "/save-one", method = RequestMethod.POST)
    @Transactional(rollbackFor = Exception.class)
    //@Async
    public String postOne( String name,  String email) {
        //C方法
//        Role role = new Role();
//        role.setRoleId(3L);
//        role.setRoleKey("8888");
//        role.setRoleSort("8888");
//        role.setStatus("0");
//        role.setRoleName(name);
//        roleService.insertRole2(role);
        //B方法
        User user = new User();
        user.setUserId(3L);
        user.setLoginName(name);
        user.setUserName(name);
        user.setEmail(email);
        userService.insertUser3(user);
        //C方法
        Role role = new Role();
        role.setRoleId(3L);
        role.setRoleKey("8888");
        role.setRoleSort("8888");
        role.setStatus("0");
        role.setRoleName(name);
        roleService.insertRole2(role);
//        int i =1;
//        i=i/0;
        return "1";

    }
}
